package com.yylending.las.common.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import butterknife.BindView;
import butterknife.ButterKnife;
import com.yyfax.common.tools.DensityUtil;
import com.yylending.las.R;
import com.yylending.las.common.widgets.CustomGridLayoutManager;
import com.yylending.las.common.widgets.Divider;
import com.yylending.las.lending.process.ProcessActivity;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by fan on 2017/6/16.
 * Copyright  2017 www.yylending.com. All Rights Reserved.
 */
public class RepaymentScheduleDialog extends Dialog {

	private final String mLoanTermStr;
	@BindView(R.id.repay_period_tv)
	TextView mRepayPeriodTv;
	@BindView(R.id.monthly_payment_amount_tv)
	TextView mMonthlyPaymentAmountTv;
	@BindView(R.id.repayment_schedule_lv)
	RecyclerView mRepaymentScheduleRv;
	private List<String> mRepaymentDatas;
	private String mMonthlyPaymentAmount;
	private String mFirstPayDate;
	private int mRepayPeriod;

	public RepaymentScheduleDialog(Context context,int repayPeriod, String monthlyPaymentAmount, String firstPayDate) {
		super(context,R.style.progress_style);
		mMonthlyPaymentAmount = monthlyPaymentAmount.replace(",","");
		mRepayPeriod = repayPeriod;
		mFirstPayDate = firstPayDate;
		mLoanTermStr = context.getString(R.string.loan_term);
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.dialog_repayment_schedule);
		ButterKnife.bind(this);
		mRepayPeriodTv.setText(String.format(mLoanTermStr, ProcessActivity.mProcess.loanTerm));
		mMonthlyPaymentAmountTv.setText(mMonthlyPaymentAmount);
		CustomGridLayoutManager LayoutManager = new CustomGridLayoutManager(getContext(), 1);
		mRepaymentScheduleRv.setLayoutManager(LayoutManager);
		mRepaymentScheduleRv.addItemDecoration(new Divider(DensityUtil.dip2px(getContext(), 15f), getContext().getResources().getColor(R.color.color_ffffff), false,
				0, 0, 0, 0));
		mRepaymentDatas = getRepaymentDatas();
		RepaymentScheduleAdapter reportDetailAdapter = new RepaymentScheduleAdapter();
		mRepaymentScheduleRv.setAdapter(reportDetailAdapter);
	}

	/**
	 * 获取还款计划的每月还款日列表
	 */
	private List<String> getRepaymentDatas() {
		List<String> repaymentScheduleDatas = new ArrayList<>();
		repaymentScheduleDatas.add(mFirstPayDate);
		String[] dates = mFirstPayDate.split("/");
		int year = Integer.valueOf(dates[0]);
		int month = Integer.valueOf(dates[1]);
		for (int i = 1; i < mRepayPeriod; i++) {
			month += 1;
			if (month > 12) {
				month -= 12;
				year += 1;
			}
			String monthStr = String.valueOf(month);
			if (1 == monthStr.length()) {
				monthStr = "0" + monthStr;
			}
			repaymentScheduleDatas.add("" + year + "/" + monthStr + "/" + dates[2]);
		}
		return repaymentScheduleDatas;
	}

	class RepaymentScheduleAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

		public RepaymentScheduleAdapter() {
			super();
		}

		@Override
		public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

			return new ViewHolder(LayoutInflater.from(getContext()).inflate(R.layout.repayment_schedule_item, parent, false));
		}

		@Override
		public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
			ViewHolder viewHolder = (ViewHolder) holder;
			viewHolder.mRepaymentDateTv.setText(mRepaymentDatas.get(position));
			viewHolder.mRepaymentAmountTv.setText(mMonthlyPaymentAmount);
		}

		@Override
		public int getItemCount() {
			return mRepaymentDatas.size();
		}

		class ViewHolder extends RecyclerView.ViewHolder {
			@BindView(R.id.repayment_date_tv)
			TextView mRepaymentDateTv;
			@BindView(R.id.repayment_amount_tv)
			TextView mRepaymentAmountTv;

			ViewHolder(View view) {
				super(view);
				ButterKnife.bind(this, view);
			}
		}
	}
}
